package magic8ball;

import java.io.*;
import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.wb.swt.*;

public class MainWindow {

	Magic8Ball magic8Ball = new Magic8Ball();

	protected Shell magicBallShell;
	Properties appSettings = new Properties();
	Cursor defaultCursor; // To change the cursor to an arrow at any point after MainWindow() has executed, use
							// shell.setCursor(defaultCursor);
	Cursor waitCursor; // To change the cursor to an hourglass at any point after MainWindow() has executed, use
						// shell.setCursor(waitCursor);
	private Composite clientArea;
	private Label answerLabel;


	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MainWindow window = new MainWindow();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();

		setPreferences();
		waitCursor = magicBallShell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT);
		defaultCursor = magicBallShell.getDisplay().getSystemCursor(SWT.CURSOR_ARROW);
		clientArea.setFocus();

		magicBallShell.open();
		magicBallShell.layout();
		while (!magicBallShell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}


	// Load application settings from .ini file
	private void setPreferences() {
		try {
			appSettings.load(new FileInputStream("appsettings.ini"));
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}

		// By default, center window
		int width = Integer.parseInt(appSettings.getProperty("width", "640"));
		int height = Integer.parseInt(appSettings.getProperty("height", "480"));
		Rectangle screenBounds = magicBallShell.getDisplay().getBounds();
		int defaultTop = (screenBounds.height - height) / 2;
		int defaultLeft = (screenBounds.width - width) / 2;
		int top = Integer.parseInt(appSettings.getProperty("top", String.valueOf(defaultTop)));
		int left = Integer.parseInt(appSettings.getProperty("left", String.valueOf(defaultLeft)));
		magicBallShell.setLocation(left, top);
		saveShellBounds();
	}


	// Save window location in appSettings hash table
	private void saveShellBounds() {
		// Save window bounds in app settings
		Rectangle bounds = magicBallShell.getBounds();
		appSettings.setProperty("top", String.valueOf(bounds.y));
		appSettings.setProperty("left", String.valueOf(bounds.x));
		appSettings.setProperty("width", String.valueOf(bounds.width));
		appSettings.setProperty("height", String.valueOf(bounds.height));
	}


	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		magicBallShell = new Shell();
		magicBallShell.setSize(512, 516);
		magicBallShell.addControlListener(new ShellControlListener());
		magicBallShell.addDisposeListener(new ShellDisposeListener());
		magicBallShell.setImage(SWTResourceManager.getImage(MainWindow.class, "/images/16x16.png"));
		magicBallShell.setText("Magic 8 Ball");
		GridLayout gl_magicBallShell = new GridLayout(1, false);
		gl_magicBallShell.marginHeight = 0;
		gl_magicBallShell.marginWidth = 0;
		magicBallShell.setLayout(gl_magicBallShell);

		clientArea = new Composite(magicBallShell, SWT.NONE);
		clientArea.addMouseListener(new ClientAreaMouse());
		clientArea.setBackgroundMode(SWT.INHERIT_DEFAULT);
		clientArea.setBackgroundImage(SWTResourceManager.getImage(MainWindow.class, "/images/500px-Magic_eight_ball.png"));
		clientArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		answerLabel = new Label(clientArea, SWT.WRAP | SWT.CENTER);
		answerLabel.setFont(SWTResourceManager.getFont("Carbon Block", 14, SWT.NORMAL));
		answerLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		answerLabel.setBounds(211, 226, 86, 65);

	}


	private class ShellDisposeListener implements DisposeListener {

		public void widgetDisposed(DisposeEvent arg0) {
			try {
				// Save app settings to file
				appSettings.store(new FileOutputStream("appsettings.ini"), "");
			} catch (Exception ex) {
			}
		}
	}


	private class ShellControlListener extends ControlAdapter {

		@Override
		public void controlMoved(ControlEvent e) {
			try {
				saveShellBounds();
			} catch (Exception ex) {
				setStatus(ex.getMessage());
			}
		}


		@Override
		public void controlResized(ControlEvent e) {
			try {
				saveShellBounds();
			} catch (Exception ex) {
				setStatus(ex.getMessage());
			}
		}
	}


	private class ClientAreaMouse extends MouseAdapter {

		@Override
		public void mouseDoubleClick(MouseEvent e) {
			answerLabel.setText(magic8Ball.getAnswer());
		}
	}


	private void setStatus(String message) {
		answerLabel.setText(message);
	}
}
